﻿@model BJG_Machining
@{
	ViewBag.Title = (Model.ID > 0 ? "修改" : "新增") + "加工订单";
}
@section header {
	<link href="~/lib/select2/select2.min.css" rel="stylesheet" asp-append-version="true" />
	<style>
		.detail-content-info, .detail-content-label {
			padding: 3px;
		}

		.select2-selection__rendered {
			color: #000 !important;
		}
	</style>
}
@section quickmenu {
	<td style="padding-right: 0px; padding-left: 10px;">
		<span style="cursor: pointer;" title="返回..." @@click="pageBack">
			<i class="icon-reply icon-2x pull-left icon-border crm-bu-style"></i>
		</span>
	</td>
}

<table style="border:0;margin:0;border-spacing:0;width:100%;padding:0 15px 10px 15px;overflow:auto;table-layout:fixed;">
	<tbody>
		<tr>
			<td style="vertical-align:top;" class="detail-content-td">
				<div style="width:100%;margin:0;padding:0;" v-show="viewid=='main'">
					<table style="width:100%;">
						<tr>
							<td style="vertical-align:top;">
								<form id="frmEdit">
									<div class="detail-panel-div" style="position:relative;overflow:auto;">
										<table border="0" cellspacing="0" cellpadding="5" style="height:42px;width:100%;table-layout:fixed;">
											<tbody>
												<tr class="detail-content-heading" style="height: 28px;">
													<td>
														<b>&nbsp;基本信息&nbsp;</b>
													</td>
												</tr>
												<tr>
													<td>
														<table border="0" cellspacing="0" cellpadding="0" style="width:100%;">
															<tr style="height: 25px">
																<td class="detail-content-label label-required" width="20%">
																	布行名
																</td>
																<td class="detail-content-info" width="30%">
																	<input type="text" id="txtKeyword" v-model="model.PartnerName" class="detailedViewTextBox validate[required]" @@keypress.prevent.enter="handleKeypressKeyword" autocomplete="off" style="ime-mode:disabled;" placeholder="输入拼音助记码/名称查找" />
																</td>
																<td class="detail-content-label" width="20%">
																	订单编号
																</td>
																<td class="detail-content-info" width="30%">
																	<input type="text" class="detailedViewTextBox" readonly v-model="model.SNo" placeholder="自动生成">
																</td>
															</tr>
															<tr>
																<td class="detail-content-label">
																	送货人
																</td>
																<td class="detail-content-info">
																	<input type="text" class="detailedViewTextBox" v-model="model.DeliveryMan">
																</td>
																<td class="detail-content-label">
																	送货日期
																</td>
																<td class="detail-content-info">
																	<input name="DeliveryDate" type="text" class="calendar validate[required]" id="DeliveryDate" style="width: 70px;cursor:pointer;" readonly maxlength="19" v-model.trim="model.DeliveryDate" />
																	<i class="icon-calendar calendar-tgr" id="DeliveryDate_tgr"></i>
																</td>
															</tr>
															<tr>
																<td class="detail-content-label label-required">
																	加工类型
																</td>
																<td class="detail-content-info">
																	<select id="MachiningType" v-model="model.MachiningType" data-value="MachiningType" class="select2 detailedViewTextBox validate[custom[option]]">
																		<option :value="o.value" v-for="(o,oidx) in machiningTypes">{{o.text}}</option>
																	</select>
																</td>
																<td class="detail-content-label">
																	卷数
																</td>
																<td class="detail-content-info">
																	<input type="number" class="detailedViewTextBox  validate[required]" v-model="model.Rolls">
																</td>
															</tr>
															<tr>
																<td class="detail-content-label">
																	米数
																</td>
																<td class="detail-content-info">
																	<input type="number" class="detailedViewTextBox validate[required]" v-model="model.Meters" @@input="handleChangeMeters">
																</td>
																<td class="detail-content-label">
																	单价
																</td>
																<td class="detail-content-info">
																	<input type="number" class="detailedViewTextBox validate[required]" v-model="model.Price" @@input="handleChangePrice">
																</td>
															</tr>
															<tr>
																<td class="detail-content-label">
																	总价
																</td>
																<td class="detail-content-info">
																	<input type="number" class="detailedViewTextBox validate[required]" v-model="model.Total">
																</td>
																<td class="detail-content-label">
																</td>
																<td class="detail-content-info">
																</td>
															</tr>
															<tr>
																<td class="detail-content-label label-required">
																	收款方式
																</td>
																<td class="detail-content-info">
																	<select id="RecvType" v-model="model.RecvType" data-value="RecvType" data-callback="handleChangeRecvType" class="select2 detailedViewTextBox validate[custom[option]]">
																		<option :value="o.value" v-for="(o,oidx) in recvtype">{{o.text}}</option>
																	</select>
																</td>
																<td class="detail-content-label">
																	支付方式
																</td>
																<td class="detail-content-info">
																	<select id="RecvWay" v-model="model.RecvWay" data-value="RecvWay" :disabled="model.RecvType!=@((int)FinancePayRecvType.现金收款)&&model.RecvType!=@((int)FinancePayRecvType.预收款)" class="select2 detailedViewTextBox validate[custom[option]]">
																		<option :value="o.value" v-for="(o,oidx) in recvway">{{o.text}}</option>
																	</select>
																</td>
															</tr>
															<tr>
																<td class="detail-content-label">
																	总欠款金额
																</td>
																<td class="detail-content-info">
																	{{model.TotalRecvLeft}}
																</td>
																<td class="detail-content-label">
																	付款金额（米）
																</td>
																<td class="detail-content-info">
																	<input type="number" class="detailedViewTextBox validate[required]" v-model="model.PreRecvTotal" placeholder="" v-show="model.RecvType!=@((int)FinancePayRecvType.记应收款)">
																</td>
															</tr>
															<tr>
																<td class="detail-content-label">
																	备注
																</td>
																<td class="detail-content-info" colspan="3">
																	<textarea v-model="model.Comment" style="height:35px;"></textarea>
																</td>
															</tr>
														</table>
													</td>
												</tr>
												<tr style="height: 38px;">
													<td style="text-align:center;">
														<button type="submit" class="pure-button pure-button-edit" onclick="$('#frmEdit').submit();return false;" style="margin-right:50px;">
															<i class="icon-check"></i>&nbsp;保存&nbsp;
														</button>
														<button class="pure-button" @@click.prevent='pageBack'>
															<i class="icon-undo"></i>&nbsp;取消&nbsp;
														</button>
													</td>
												</tr>
											</tbody>
										</table>
									</div>
								</form>
							</td>
						</tr>
					</table>
				</div>
			</td>
		</tr>
	</tbody>
</table>
@section footer {
	<script src="~/js/related/sys_attachment.js" asp-append-version="true"></script>
	<script src="~/js/pinyin.js" asp-append-version="true"></script>
	<script src="~/lib/jquery.autocomplete-1.4.10.js" asp-append-version="true"></script>
	<script src="~/lib/math-6.1.0.min.js" asp-append-version="true"></script>
	<script src="~/lib/select2/select2.min.js" asp-append-version="true"></script>
	@*日期必须开始*@
	<script src="~/lib/laydate/laydate.js" asp-append-version="true"></script>
	<script src="~/lib/moment/moment.min.js" asp-append-version="true"></script>
	@*日期必须结束*@
	@*验证开始*@
	<script src="~/lib/validationEngine/jquery.validationEngine.min.js" asp-append-version="true"></script>
	<script src="~/lib/validationEngine/jquery.validationEngine-zh_CN.js" asp-append-version="true"></script>
	<link href="~/lib/validationEngine/validationEngine.jquery.css" rel="stylesheet" asp-append-version="true" />
	@*验证结束*@
	@*列表相关开始*@
	<script type="text/javascript">
		var pageEnumData = {
			state:@(Json.Serialize(ViewBag.StateOptions)),
			machiningTypes:@(Json.Serialize(ViewBag.MachiningTypes)),
			recvtype:@(Json.Serialize(ViewBag.RecvTypeOptions)),
			recvway:@(Json.Serialize(ViewBag.RecvWayOptions)),
		};
		var modelJson=@(Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model, new Newtonsoft.Json.JsonSerializerSettings()
		   {
			   DateFormatString = "yyyy-MM-dd HH:mm:ss"
		   })));
		var modelData = $.extend(modelJson, {});
		if(modelData.Rolls==0){
			modelData.Rolls="";
		}
		if(modelData.Meters==0){
			modelData.Meters="";
		}
		if(modelData.Price==0){
			modelData.Price="";
		}
		var pageData = {
			hideBreadcrumb: false,
			machiningTypes: pageEnumData.machiningTypes,
			recvtype: pageEnumData.recvtype,
			recvway: pageEnumData.recvway,
			serviceCode: '@(ViewBag.ServiceCode)',
			global: globalData,
			viewid: 'main',
			related: @(Json.Serialize(ViewBag.RelatedModules)),
			model:modelData
		};

		var pageApp = new Vue({
			el: '#app',
			data: pageData,
			created: function () {
			},
			mounted: function () {
			},
			methods: {
				pageBack: function() {
					//history.go(-1);
					if (@(Model.ID > 0 ? "true" : "false")) {
						history.back();
					} else {
						location.href = '@(Url.Action("List", "Machining"))?nc=' + new Date().getTime();
					}
				},
				pageSave: function() {
					utils.ajaxPost('@(Url.Action("Save", "Machining"))', this.model, function (resp) {
						utils.success("保存成功");
						location.href = '@(Url.Action("ViewDetail", "Machining", new { id = "" }))/'+resp.data+'?nc=' + new Date().getTime();
					}, function (msg) {
						utils.alert(msg);
					});
				},
				relatedChange: function (vid, e) {
					switch (e.action) {
						case "total":
							{
								this.related = this.related.map(function (item) {
									if (item.id == vid) {
										return Object.assign({}, item, { count: e.data });
									} else {
										return item;
									}
								});
							}
							break;
					}
				},
				handleChangeRecvType: function (value,text,e) {
					if (value != "@((int)FinancePayRecvType.预收款)" && value != "@((int)FinancePayRecvType.现金收款)") {
						$('#RecvWay').val('0').trigger('change');
					}
				},
				handleKeypressKeyword: function (e) {
				},
				calcTotal: function (para1, para2) {
					para1 = para1 || 0;
					para2 = para2 || 0;
					var result;
					result = math.bignumber(para1).mul(math.bignumber(para2));
					return math.format(result, { notation: 'fixed', precision: 2 });
				},
				handleChangeMeters: function () {
					this.model.Total = this.calcTotal(this.model.Meters,this.model.Price);
				},
				handleChangePrice: function () {
					this.model.Total = this.calcTotal(this.model.Meters,this.model.Price);
				},
			},
			computed: {
				currentM: function () {
					var currentM = null;
					var i;
					var j;
					for (i = 0; i < this.global.modules.length; i++) {
						for (j = 0; j < this.global.modules[i].Services.length; j++) {
							if (this.serviceCode == this.global.modules[i].Services[j].Code) {
								currentM = this.global.modules[i];
								break;
							}
						}
					}
					return currentM;
				},
				currentS: function () {
					var currentS = null;
					var i;
					var j;
					for (i = 0; i < this.global.modules.length; i++) {
						for (j = 0; j < this.global.modules[i].Services.length; j++) {
							if (this.serviceCode == this.global.modules[i].Services[j].Code) {
								currentS = this.global.modules[i].Services[j];
								break;
							}
						}
					}
					return currentS;
				}
			}
		});

		$(document).ready(function () {
			$('select.select2').each(function (eidx, elem) {
				var $select = $(elem);
				if ($select.data('select2')) {
					$select.select2('destroy');
				}
				$select.select2({
					minimumResultsForSearch: -1,
				});
				$select.on("change.select2", function (e) {
					pageApp.model = Object.assign({}, pageApp.model,
						$select.data("text") ? _defineProperty({}, $select.data("text"), $select.find('option:selected').text()) : {},
						$select.data("value") ? _defineProperty({}, $select.data("value"), $select.val()) : {},
					);
					$select.data("callback") && pageApp[$select.data("callback")] && pageApp[$select.data("callback")](e);
				});
			});
			$('.calendar-tgr').each(function (eidx, elem) {
				var $this = $(elem);
				var input = $this.prev('input')[0];
				$this.click(function () {
					laydate.render({
						elem: input
						, show: true //直接显示
						, closeStop: $this[0]
						, done: function (value, date) {
							modelData[input.name]=value;
							// pageData.listConditions = Object.assign({}, pageData.listConditions, _defineProperty({}, input.name, value));
						}
					});
				});
			});

			$('.calendar').each(function (eidx, elem) {
				var $this = $(elem);
				var input = $this[0];
				laydate.render({
					elem: input
					, closeStop: $this.next('i')[0]
					, done: function (value, date) {
						// pageData.listConditions = Object.assign({}, pageData.listConditions, _defineProperty({}, input.name, value));
							modelData[input.name]=value;
					}
				});
			});
			$("#frmEdit").keypress(function (e) {
				if (e.keyCode == 13) {
					return false;
				}
			}).validationEngine({
				scroll: false,
				validateNonVisibleFields: false,
				autoHidePrompt: true,
				showOneMessage: true,
				ajaxFormValidationMethod: 'post',
				onValidationComplete: function (form, status) {
					if (!status) {
						return;
					}
					pageApp.pageSave();
				}
			});

			var uinfoJson = localStorage.getItem('uinfo');
			var uinfo = uinfoJson ? JSON.parse(uinfoJson) : {};

			$('#txtKeyword').autocomplete({
				serviceUrl: '@(Url.Action("Autocomplete", "Partner"))',
				params: { limit: '' },
				ajaxSettings: {
					headers: {
						skey: uinfo.skey || "",
						ucode: uinfo.ucode || ""
					}
				},
				//groupBy: 'catname',
				// width:600,
				formatResult: function (suggestion, value, i) {
					return '<a>\
						<span style="color:#000;">'+ (suggestion.Name || "") + '</span>\
					</a>';
				},
				onSearchStart: function (params) {
				},
				onSelect: function (suggestion) {
					this.value = suggestion.Name;
					modelData.PartnerID = suggestion.ID;
					modelData.PartnerName = suggestion.Name;
					statistics(suggestion.ID);
				}
			});
		});
		$(function () {
			if(isMobile())
			{
					window.location.href = '@(Url.Action("MobileEdit", "Machining"))/' + modelData.ID + '?nc=' + new Date().getTime();
			}
		});

		function isMobile() {
		  const regExp = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
		  return regExp.test(navigator.userAgent);
		}

		function statistics(fromID) {
			utils.ajaxPost('@(Url.Action("Statistics", "FinanceRecvDue"))', { partnerid: fromID}, function (resp) {
					if(resp&&resp.r&&resp.data)
					{
						modelData.TotalRecvLeft = resp.data.TotalRecvLeft;
					}
			}, function (msg) {
			});
		}
	</script>
	@*列表相关结束*@
}